import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class StreamDemo {
    public static void main(String[] args) {
        // https://juejin.cn/post/6844903830254010381
        List<String> list = getList();
        List<Person> persons = getPersons();
        for (Person person : persons) {
            System.out.println("person = " + person);
        }
        Stream.of("d2", "a2", "b1", "b3", "c")
                .map(s -> {
                    System.out.println("map1: " + s);
                    return s.toUpperCase(); // 转大写
                })
//                .sorted((s1, s2) -> {
//                    System.out.printf("sort: %s; %s\n", s1, s2);
//                    return s1.compareTo(s2); // 排序
//                })
                .distinct()
                .skip(1)
                .map(s -> {
                    System.out.println("map2: " + s);
                    return s.toLowerCase();
                })
                .filter(s -> {
                    System.out.println("filter: " + s);
                    return s.startsWith("a"); // 过滤出以 a 为前缀的元素
                })
                .forEach(s -> System.out.println("forEach: " + s)); // for 循环输出

    }


    private static List<Person> getPersons() {
        List<Person> persons = Arrays.asList(new Person("Max", 18),
                new Person("Peter", 23),
                new Person("Pamela", 23),
                new Person("David", 11));
        return persons;
    }


    private static List<String> getList() {
        List<String> list = new ArrayList<>();
        for (int i = 1; i <= 35; i++) {
            list.add(String.valueOf(i));
        }
        return list;
    }
}

class Person {
    String name;
    int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

@Data
class Foo {
    private String name;
    private List<Bar> bars = new ArrayList<>();

    public Foo(String name) {
        this.name = name;
    }
}

@Data
class Bar {
    private String name;

    public Bar(String name) {
        this.name = name;
    }
}

class Outer {
    Nested nested;
}

class Nested {
    Inner inner;
}

class Inner {
    String foo;
}

